<template>
  <div class="wrapper">
    <div class="left_chars" ref="chars">
      <canvas></canvas>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import customed from '../../../assets/common/customed'

export default {
  props: {
    title: String,
    data: Array
  },
  data() {
    return {
      echarsData: {
        title: {
          text: '',
          left: "left",
          padding: [0,20,0,15],
          textStyle: {
            color: "#fff",
            fontWeight: 600,
            fontSize: 18,
            lineHeight: 40,
          }
        },
        tooltip: {
          trigger: "item",
          formatter: "{b}:{c}"
        },
        legend: {
          orient: "horizontal",
          top: '2%',
          left: '25%',
          data: [],
          textStyle: {
            color: "#fff"
          }
        },
        series: [
          {
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            data: [],
            label: {
              normal: {
                show: true,
                formatter: '{b}:{c}\n'},
                labelLine :{show:true}
              }, 
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      },
      myChart: {}
    };
  },
  computed: {
    ...mapGetters(["resize"])
  },
  watch: {
    resize(val) {
      this.againRender();
    },
    data:{
      deep: true,
      handler: function(val){
        this.echarsData.legend.data = val.name;
        this.echarsData.series[0].data = val
        this.init();

      }
    }
  },
  methods: {
    init() {
      this.echarsData.title.text = this.title
      let dom = this.$refs.chars;
      this.myChart = this.$echarts.init(dom,'customed');
      this.myChart.setOption(this.echarsData);
    },
    againRender() {
      let dom = this.$refs.chars;
      this.myChart.resize();
    },
   
  },
  beforeDestroy(){
    this.myChart = null;
    this.echarsData = null;
  }
};
</script>

<style lang="less" scoped>
.wrapper {
  .left_chars {
    background-color: rgba(0, 0, 0, 0.2);
    width: 23.5vw;
    height: 40vh;
  }
}
</style>